import { request } from "@/utils/request";
import { mockUrlPrefix } from "@/api/baseUrl";

// 字典类型接口

export function getDictTypeList(params) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/type/list`,
    params,
  });
}

export function addDictType(data) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/type`,
    method: "post",
    data,
  });
}

export function updateDictType(data) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/type`,
    method: "put",
    data,
  });
}

export function deleteDictType(data) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/type`,
    method: "delete",
    data: {
      id: data,
    },
  });
}

// 字典数据接口

// 根据字典类型查找字典数据
export function getDictByType(value) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/data?dictType=${value}`,
    method: "get",
    transformResponse: (res) => {
      const result = JSON.parse(res)
      result.data = result.data.map(item => ({
        label: item.dictLabel,
        value: item.dictValue,
      }))
      return result
    }
  });
}

export function getDictDataList(params) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/data/list`,
    params,
  });
}

export function addDictData(data) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/data`,
    method: "post",
    data,
  });
}

export function updateDictData(data) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/data`,
    method: "put",
    data,
  });
}

export function deleteDictData(data) {
  return request({
    url: `${mockUrlPrefix}/api/system/dict/data`,
    method: "delete",
    data: {
      id: data,
    },
  });
}